package us.sunrisemorning.pixivbrower.spider;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Aop;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

public class DbPipeline implements Pipeline {

    private DbQueue queue = Aop.get(DbQueue.class);

    private SpiderStatus status;

    public DbPipeline(SpiderStatus status) {
        this.status = status;
    }

    @Override
    public void process(ResultItems resultItems, Task task) {
        if (resultItems.isSkip()) {
            return;
        }

        String result = resultItems.get("result");
        JSONArray array = JSON.parseArray(result);

        array.forEach(item -> {
            JSONObject o = (JSONObject) item;
            
            queue.put(o);

            status.addSum();
        });
    }

}
